package com.neo.service;

import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.neo.entity.credit.CreditMessage;
import com.neo.entity.credit.CreditRepayPlan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.neo.util.DateUtil;
import com.neo.util.DjsStringUtil;
import com.neo.util.ReadFromFile;


@Service
public class TransferRepayPlanService {

    protected static Logger logger= LoggerFactory.getLogger(TransferRepayPlanService.class); 
    
	@Autowired
    private JdbcTemplate jdbcTemplate;

	
	//读取刘峰给的借款人信息excel,生成标的
	public void insertRepayPlan() throws FileNotFoundException{
		List listCsv = ReadFromFile.readFileByLinesGbk(DjsStringUtil.PLAN_FILE_PATH);
		List listCreditCvs = ReadFromFile.readFileByLinesGbk(DjsStringUtil.IMPORT_FILE_PATH);
		List<CreditMessage> creditMessageList = new ArrayList<>();
		List<CreditRepayPlan> creditRepayPlanList = new ArrayList<>();
		for (int i = 1; i < listCsv.size(); i++) {
			String[] s = (String[])listCsv.get(i);
			CreditRepayPlan creditRepayPlan = new CreditRepayPlan();
			creditRepayPlan.setTurnOverNo(s[0]);
			creditRepayPlan.setCurrentTerm(s[1]);
			creditRepayPlan.setDueDate(s[2]);
			creditRepayPlan.setPrincipal(s[3]);
			creditRepayPlan.setInterest(s[4]);
			String repay_plan_status = s[5];
			switch (repay_plan_status) {
				case "待还":
					repay_plan_status="WTRP";
					break;
				case "逾期":
					repay_plan_status="REXP";
					break;
				case "正常还完":
					repay_plan_status="NMRF";
					break;
				case "逾期还完":
					repay_plan_status="EPRF";
					break;
				case "提前还款还完":
					repay_plan_status="PPRF";
					break;
				default:
					break;
			}
			creditRepayPlan.setRepay_plan_status(repay_plan_status);
			creditRepayPlanList.add(creditRepayPlan);
		}
		for (int i = 1; i < listCreditCvs.size(); i++) {
			String[] s = (String[])listCsv.get(i);
			CreditMessage creditMessage = new CreditMessage();
			creditMessage.setMakeLoanTime(s[0]);
			creditMessage.setProjectNo(s[1]);
			creditMessage.setTurnOverNo(s[1]);
			creditMessage.setTotalTrem(s[15]);
			creditMessageList.add(creditMessage);
		}
		for (CreditMessage creditMessage:creditMessageList){
			String projectNo = creditMessage.getTurnOverNo();
			for (CreditRepayPlan creditRepayPlan: creditRepayPlanList){
				if (creditRepayPlan.getTurnOverNo().equals(projectNo)){
					String sql =" insert into jx_loan_repay_plan"
							+ " (PROJECT_NO,CURRENT_TERM,DUE_DATE,PRINCIPAL,INTEREST,REPAY_PLAN_STATUS,  "
							+ "TOTAL_TERM,"
							+ "TOTAL_AMT,"
							+ "ACCOUNT_ID,USERNAME"
							+ ")"
							+ " values (?,?,?,?,?,?,"
							+ "?,"
							+ "0,"
							+ "'','')";
					jdbcTemplate.update(sql,projectNo,creditRepayPlan.getCurrentTerm(),creditRepayPlan.getDueDate(),creditRepayPlan.getPrincipal(),creditRepayPlan.getInterest(),
					creditRepayPlan.getRepay_plan_status(),creditMessage.getTotalTrem());
				}
			}
		}
	}
	//更新还款计划
	public void updatePlanTotalTerm() throws FileNotFoundException{
		List listCsv = ReadFromFile.readFileByLines(DjsStringUtil.IMPORT_FILE_PATH);
    	for (int i = 1; i < listCsv.size(); i++) {
			String[] s = (String[])listCsv.get(i);
			String project_no = s[1];//标的编号
			String loan_term = s[15];//借款期限
	    	String sql =" UPDATE jx_loan_repay_plan SET  loan_term=? where project_no =? ;";
	    	int j = jdbcTemplate.update(sql,loan_term,project_no);
		}
	}
	
}
